package com.wmlive.hhvideo.heihei.db.manager;

import android.content.Context;

import com.wmlive.hhvideo.heihei.db.datatable.DaoMaster;
import com.wmlive.hhvideo.heihei.db.datatable.DaoSession;
import com.wmlive.hhvideo.heihei.db.datatable.IMMessageUserTableDao;
import com.wmlive.hhvideo.heihei.login.AccountUtil;
import com.wmlive.hhvideo.utils.KLog;

/**
 * 用户数据库管理
 * Created by kangzhen on 2017/7/6.
 */

public class GreenDaoUserDBManager {
    private  DaoMaster mDaoMaster;
    private  DaoSession mDaoSession;

    private Context mContext;

    private IMMessageUserTableDao imMessageUserDao;

    protected GreenDaoUserDBManager(Context context) {
        mContext = context;
    }
    private void getGreenDaoUserIMDao(){
        DaoSession daoSession = getDaoSessionUser(mContext);
        imMessageUserDao = daoSession.getIMMessageUserTableDao();
    }


    /**
     * 获取Daomaster
     * @return
     */
    private  DaoMaster getDaoMasterUser(Context context){
        if(mDaoMaster==null){
            String strUserId = String.valueOf(AccountUtil.getUserId());
            KLog.e("db_manager","---------------db name is userId :"+strUserId);
            DaoMaster.DevOpenHelper openHelperDefault =  new DaoMaster.DevOpenHelper(context, "hhvideo_"+strUserId+"_db.db");
            mDaoMaster = new DaoMaster(openHelperDefault.getWritableDatabase());
        }
        return mDaoMaster;
    }

    /**
     * 获取DaoSessin
     * @return
     */
    private  DaoSession getDaoSessionUser(Context context){
        if (mDaoSession == null)
        {
            if (mDaoMaster == null)
            {
                mDaoMaster = getDaoMasterUser(context);
            }
            mDaoSession = mDaoMaster.newSession();
        }
        return mDaoSession;
    }
    /**
     * 返回管理消息中的默认数据库
     * @return
     */
    public IMMessageUserTableDao getIMMessageUserTableDao() {
        if(imMessageUserDao==null){
            getGreenDaoUserIMDao();
        }
        return  imMessageUserDao;
    }
}
